<?php
/**
 * Created by PhpStorm.
 * User: Cansnow
 * Date: 2015-05-13
 * Time: 22:56
 */
namespace ORG;
//use \PhpOffice\PhpWord\PHPWord;
//use \PhpOffice\PhpWord;
class Rec2File{
    /**
     * 
     */
    static function create($id=null,$type='all',$refresh=false){
        $ret = [];
        $vo = model('Loan')->getView(['id'=>$id]);        
        $ret['realname'] = $vo['realname'];
        $ret['idcard'] = $vo['extra']['idcard'];        
        $attachs = json_decode($vo['attachs'],true);
        $BasePath = "/Uploads/danbao/".$vo['id'].'/';
        $RootPath = \Env::get('root_path').'/public_html';

        if(in_array($type,['word','all'])){
            $docxFn = $BasePath.$ret['realname'].' '.$ret['idcard'].'.docx';
            if(!file_exists($RootPath.$docxFn) || $refresh==true){
                $docxFn = self::toWord($id,$BasePath);
            }
            $ret['word'] = $docxFn;
        }
        if(in_array($type,['pdf','all'])){
            $pdfarr=[
                'zxpdf'=>[
                    'name'  =>  $ret['realname'].' - 征信资料.pdf'  ,
                    'files' => ['身份证复印件','银行征信'],
                ],
                'qtpdf'=>[
                    'name'  =>  $ret['realname'].' - 其他资料.pdf'  ,
                    'files' => ['手持照片','鹏元征信'],
                ],
            ];
            foreach ($pdfarr as $k => $v) {
                $fn = $BasePath.$v['name'];
                if(!file_exists($RootPath.$fn) || $refresh==true){
                    $pdf = new \TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);  
                    $pdf->setPrintHeader(false);
                    $pdf->setPrintFooter(false);
                    foreach ($v['files'] as $k1 => $v1) {
                        $pdf->AddPage();
                        $pdf->Image('.'.$BasePath.$attachs[$v1][0],null,null,null,null,'JPG','','center',true,300,'C');
                    }
                    $pdf->Output($RootPath.iconv('UTF-8','GB2312//IGNORE',$fn), 'F');
                }
                $ret[$k] = $fn;
            }
        }
        return $ret;
    }
    static function create_other($id=null,$type="yuqichufa",$path='/Uploads/danbao/'){
        $root_path = \Env::get('root_path').'/public_html';
        if(!$id){exit('ID NOT EXISTS');}
        $vo = model('Loan')->getView(['id'=>$id]);
        $PHPWord = new \PhpOffice\PhpWord\PhpWord();
        if($type=="yuqichufa"){
            $arr=[];
            $document = $PHPWord->loadTemplate($root_path.'/static/danbao/yuqichufa_tpl.docx');
            $arr['realname'] = $vo['realname'];
            $arr['idcard'] = $vo['extra']['idcard'];
            $arr['today'] = date('Y年m月d日');
            $arr['no'] = array_key_exists('no',$vo) ? $vo['no'] : "00".$vo['id'];
            foreach ($arr as $key => $value) {
                $document->setValue('{='.$key.'}' , $value);
            }
            $fn = '不可撤销的逾期还款处罚同意书.docx';
        }else if($type=="tuijianhan"){
            $arr=[];
            $document = $PHPWord->loadTemplate($root_path.'/static/danbao/tuijianhan_tpl.docx');
            $arr['realname'] = $vo['realname'];
            $arr['money'] = tocnupper($vo['money']*10000);
            $arr['no'] = array_key_exists('no',$vo) ? $vo['no'] : "00".$vo['id'];
            foreach ($arr as $key => $value) {
                $document->setValue('{='.$key.'}' , $value);
            }
            $fn = '同意贷后服务通知书.docx';
        }
        if($path{0}=='.'){
            $path.= substr($path,1);
        }
        if($path{0}!='/'){
            $path.= '/'.$path;
        }
        $basePath=$root_path.$path.$vo['id'].'/';
        if(!file_exists($basePath)){
            mkdir($basePath,0777,true);
        }
        $document->saveAs($basePath.iconv('UTF-8','GB2312//IGNORE', $fn),true);
        return ['path'=>$path.$vo['id'].'/'.$fn];

    }
    static function toWord($id=null,$path='/Uploads/danbao/'){
        if(!$id){exit('ID NOT EXISTS');}
        //require_once EXTEND_PATH.'\PhpOffice\PhpWord\PHPWord.php';
        $arr = model('Loan')->getView(['id'=>$id])->toArray();
        $arr = array_merge($arr,$arr['extra']);
        $fn = $arr['realname'].' '.$arr['extra']['idcard'].'.docx';
        $arr['year']= ($arr['month'] ? $arr['month'] : 24)/12;
        $arr['use'] = $arr['extra']['car_brand'].' '.$arr['extra']['car_model'].($arr['extra']['car_info'] ? '('.$arr['extra']['car_info'].')' : '');
        $PHPWord = new \PhpOffice\PhpWord\PhpWord();

        $arr['money'] = $arr['money'];
        $arr['email'] = '';
        $arr['marry'] = self::getMarry($arr['extra']['marry']);
        $arr['sex'] = self::getSex($arr['extra']['sex']);
        $arr['hkszd'] = self::getHkszd($arr['extra']['hkszd']);
        $arr['edu'] = self::getEdu($arr['extra']['edu']);
        $arr['bdfc'] = self::getBdfc($arr['extra']['bdfc']);
        $arr['jzzk'] = self::getJzzk($arr['extra']['jzzk']);
        $arr['sos_rela1'] = self::getSosRela1($arr['extra']['sos_rela1']);
        $arr['sos_rela2'] = self::getSosRela2($arr['extra']['sos_rela2']);
        $arr['sos_sex1'] = Linkage($arr['extra']['sos_sex1'],'Sex');
        $arr['sos_sex2'] = Linkage($arr['extra']['sos_sex2'],'Sex');
		if(strlen($arr['extra']['mate_idcard'])==18){
			for ($i=0; $i < strlen($arr['extra']['mate_idcard']); $i++) { 
				$arr['po'.($i+1)]=$arr['extra']['mate_idcard']{$i};
			}
		}else{
			for ($i=0; $i < 18; $i++) { 
				$arr['po'.($i+1)]='';
			}
		}
        for ($i=0; $i < strlen($arr['extra']['idcard']); $i++) { 
            $arr['id'.($i+1)]=$arr['extra']['idcard']{$i};
        }
        $document = $PHPWord->loadTemplate(\Env::get('root_path').'/public_html/static/shenqingbiao_tpl.docx');
        foreach ($arr as $key => $value) {
			//echo $key,',',$value,'<br />';
            $document->setValue('{='.$key.'}' , $value);
        }
        if($path{0}=='.'){
            $path.= substr($path,1);
        }
        if($path{0}!='/'){
            $path.= '/'.$path;
        }
        $document->saveAs(\Env::get('root_path').'/public_html'.$path.iconv('UTF-8','GB2312//IGNORE', $fn),true);
		//P($document);
		//exit;
        return $path.$fn;
    }
    static function getSosRela1($m=0){
        switch ($m) {
            case '父亲':
            case '母亲':
            case '父母':
            case '1':
                $s = '■父母 □子女 □其他亲属';
                break;
            case '子女':
            case '儿子':
            case '女儿':
            case '2':
                $s = '□父母 ■子女 □其他亲属';
                break;
            default:
                $s = '□父母 □子女 ■其他亲属('.$m.')';
                break;
        }
        return $s;
    }
    static function getSosRela2($m=0){
        switch ($m) {
            case '朋友':
            case '1':
                $s = '■朋友 □同学 □其他';
                break;
            case '同学':
            case '2':
                $s = '□朋友 ■同学 □其他';
                break;
            default:
                $s = '□朋友 □同学 ■其他('.$m.')';
                break;
        }
        return $s;
    }
    static function getJzzk($m=0){
        switch ($m) {
            case 'e':
                $s = '□自建 □自购无按揭 □按揭 □租用 ■亲属或单位住房';
                break;
            case 'c':
                $s = '□自建 □自购无按揭 ■按揭 □租用 □亲属或单位住房';
                break;
            case 'b':
                $s = '□自建 ■自购无按揭 □按揭 □租用 □亲属或单位住房';
                break;
            case 'a':
                $s = '■自建 □自购无按揭 □按揭 □租用 □亲属或单位住房';
                break;
            default:
                $s = '□自建 □自购无按揭 □按揭 ■租用 □亲属或单位住房';
                break;
        }
        return $s;
    }

    static function getBdfc($m=0){
        switch ($m) {
            case '有':
            case 'a':
                $s = ' ■有  □无';
                break;
            default:
                $s = ' □有  ■无';
                break;
        }
        return $s;
    }
    static function getEdu($m=0){
        switch ($m) {
            case '大专':
            case 'c':
                $s = '□硕士及以上  □本科  ■大专  □高中及以下';
                break;
            case '本科':
            case 'b':
                $s = '□硕士及以上  ■本科  □大专  □高中及以下';
                break;
            case '硕士及以上':
            case 'a':
                $s = '■硕士及以上  □本科  □大专  □高中及以下';
                break;          
            default:
                $s = '□硕士及以上  □本科  □大专  ■高中及以下';
                break;
        }
        return $s;
    }

    static function getMarry($m=0){
        switch ($m) {
            case '已婚':
            case '2':
                $s = '□未婚 ■已婚 □离婚 □丧偶';
                break;
            case '离婚':
            case '3':
                $s = '□未婚 □已婚 ■离婚 □丧偶';
                break;
            case '丧偶':
            case '4':
                $s = '□未婚 □已婚 □离婚 ■丧偶';
                break;          
            default:
                $s = '■未婚 □已婚 □离婚 □丧偶';
                break;
        }
        return $s;
    }
    static function getSex($m=0){
        switch ($m) {
            case '女':
            case '2':
                $s = ' □男    ■女';
                break;
            default:
                $s = ' ■男    □女';
                break;
        }
        return $s;
    }
    static function getHkszd($m=0){
        switch ($m) {
            case '本地':
            case 'a':
                $s = ' □非本地    ■本地';
                break;
            default:
                $s = ' ■非本地    □本地';
                break;
        }
        return $s;
    }

}